<?php

namespace App\Service\Message\v1_1_4;

use App\Constants\ErrorCode;
use App\Helpers\ImUtil;
use App\Helpers\RedisUtil;
use App\Model\GiftLog;
use App\Model\Message;
use App\Model\User;
use App\Model\UserBlacklist;
use App\Model\UserChat;
use App\Model\UserMsgLog;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 聊天框信息
 */
class ChatInfoService extends BaseService
{
    //单向聊天限制
    protected $msgLmt = 3;

    protected function RoVerifyList()
    {
        return [
            'chat_im_account' => 'required|string'
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $user = $this->getUser();
        $chatImAccount = $this->request->input('chat_im_account', '');
        $userIds = User::getUserIdByIm($chatImAccount, 'id');
        $chatUserId = $userIds[0] ?? 0;
        if (!$chatUserId || $chatUserId == $userId) {
            return $this->setError(ErrorCode::USER_NOT_VALID, '参数传入有误~');
        }
        $userProfile = UserProfile::getProfile($chatUserId);
        //自己是否声优
        $isHost = UserProfile::checkIsHost($userId);

        //聊友用户信息
        $ret['self_user_id'] = $userId;
        $ret['is_host'] = $isHost;
        $ret['chat_user_id'] = $chatUserId;
        $ret['user_info'] = UserProfile::buildProfileDetail($userProfile);

        //是否被对方拉黑
        $ret['is_blocked'] = UserBlacklist::check($chatUserId, $userId) ? 1 : 0;
        //是否拉黑对方
        $ret['is_blocking'] = UserBlacklist::check($userId, $chatUserId) ? 1 : 0;
        //是否展示语聊按钮
        $ret['can_call'] = $this->platform == 1 ? 0 : UserProfile::checkCanCall($ret['user_info']['role'], $userProfile->permit);
        //是否展示视频按钮
        $ret['can_video'] = $this->platform == 1 ? 0 : UserProfile::checkCanVideo($ret['user_info']['role'], $userProfile->video_permit);

        //小助手消息回复
        $this->officialFeedBack($user->im_account, $chatImAccount);

        //官方小助手
        if (User::checkOfficial($chatUserId) || User::checkOfficial($userId)) return $ret;

        //声优无限制
        if ($isHost) {
            //礼物过期提示
            $ret['exp_num'] = GiftLog::expireRecords($chatUserId, $userId);
            // if ($ret['exp_num'] > 0) {
            // ImUtil::sendInfoTipMsg($user->im_account, '您没有及时回复消息，有打招呼礼物已被退回～', $chatImAccount);
            // }
            $ret['chat_charge'] = 1;
            log_user_result($userId, $ret);
            return $ret;
        }

        //私聊是否收费
        $coinChat = $userProfile->chat_coin_setting;
        $checkCharge = UserProfile::checkRoleHost($userProfile->role) && $coinChat > 0;
        // $ret['chat_charge'] =  $checkCharge ? 1 : 0;

        //发送系统提示
        // $msgNumLmt = UserMsgLog::checkChargeBeginNum($userId) >= 10;
        // $lockKey = "chat_charge_tip_{$userId}_{$chatUserId}";
        // if ($checkCharge && $msgNumLmt && !RedisUtil::concurrentLock($lockKey, 3600 * 240)) {
        //     $tip = '与认证主播聊天，您需要支付 <font color="#F55363">' . $coinChat . '金币/条</font>';
        //     Message::sendImSystemTip($userId, $chatUserId, $tip, 2);
        // }

        //所有开放检测
        $ret['chat_charge'] = 1;

        // log_user_result($userId, $ret);
        return $ret;
    }

    /**
     * 小助手消息回复
     */
    function officialFeedBack($fromImAccount = '', $toImAccount = '')
    {
        $officialAccount = env('IM_ACCOUNT_KEFU', '');
        if ($toImAccount != $officialAccount) return false;
        $key = "Official_auto_{$fromImAccount}";
        $lockTime = check_dev_env() ? 30 : 3600;
        if (RedisUtil::concurrentLock($key, $lockTime)) return false;
        $msg = '小助手在，有问题随时找我～';
        ImUtil::sendTextMsg($fromImAccount, $msg, $officialAccount);
    }
}
